package com.example.jack.community.view.dynamic;

import android.app.SearchManager;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.crazypudding.greendaodemo.greendao.dao.CompanyDao;
import com.crazypudding.greendaodemo.greendao.dao.DaoMaster;
import com.crazypudding.greendaodemo.greendao.dao.DaoSession;
import com.example.jack.community.R;
import com.example.jack.community.view.dynamic.dynamic_bean.ListAdapter;
import com.example.jack.community.view.dynamic.dynamic_data.Company;

import java.util.List;

public class DynamicSearch extends AppCompatActivity implements View.OnClickListener {
    private CompanyDao mCompanyDao;
    private List<Company> mEmployeeList;
    private List<Company> mQueryList;
    private ListAdapter mQueryAdapter;
    private ListAdapter mAdapter;
    private SearchView sv_search;
    private ListView mListView;
    private TextView tv_clear;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );

        setContentView( R.layout.activity_dynamic_search );
        findViewsa();
        initDbHelp();//初始化数据库
        initList();//初始化List
        handleIntent(getIntent());
    }
    //初始化List
    private void initList() {
        mEmployeeList = mCompanyDao.queryBuilder().list();
        mListView = (ListView) findViewById(R.id.list_view);//Set listView
        mAdapter = new ListAdapter(this, R.layout.dynamic_list, mEmployeeList);
        mListView.setAdapter(mAdapter);
        //List每个列别的点击事件
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

//                Toast.makeText(DynamicSearch.this, mEmployeeList.get(i).getName().toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }
    //初始化数据库
    private void initDbHelp() {
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "recluse-db", null);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(db);
        DaoSession daoSession = daoMaster.newSession();
        mCompanyDao = daoSession.getCompanyDao();
    }


    private void findViewsa() {
        sv_search = (SearchView) findViewById( R.id.sv_search );
        tv_clear = (TextView) findViewById( R.id.tv_clear );
        tv_clear.setOnClickListener( this );

        // 设置搜索文本监听
        sv_search.setOnQueryTextListener( new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                //搜索时触发事件
                mName = query;
                mCompanyDao.insert(new Company(null, mName));
                refreshList();
                tv_clear.setText("清除历史记录");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
//                搜索时根据文本框动态改变搜索内容
                if (!TextUtils.isEmpty( newText )) {
//                    lv_record.setFilterText(newText);//随输入内容显示出来
                } else {
                    mListView.clearChoices();
                }
                return false;
            }
        } );
    }

    //刷新列表视图显示最新数据
    private void refreshList() {
        mEmployeeList.clear();
        mEmployeeList.addAll(mCompanyDao.queryBuilder().list());
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_clear:
                mCompanyDao.deleteAll();//清理数据库数据
                refreshList();//时时跟新数据
                tv_clear.setText("无历史记录");
                break;
        }
    }


    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            mQueryList = mCompanyDao.queryBuilder().where(CompanyDao.Properties.Name.eq(query)).list();

            //实例化适配器,用于保存查询结果显示在查询列表中
            mQueryAdapter = new ListAdapter(this, R.layout.dynamic_list, mQueryList);
            mListView.setAdapter(mQueryAdapter);
        }
    }
}
